<!DOCTYPE html>
<html lang="zh-CN">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>注册 - 手机商城</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
            background: linear-gradient(135deg, #2c3e50 0%, #34495e 100%);
            min-height: 100vh;
            display: flex;
            align-items: center;
            justify-content: center;
            color: #2c3e50;
        }

        .back-home {
            position: absolute;
            top: 2rem;
            left: 2rem;
            color: white;
            text-decoration: none;
            font-weight: 500;
            opacity: 0.9;
            transition: opacity 0.3s;
        }

        .back-home:hover {
            opacity: 1;
        }

        .register-container {
            background: white;
            border-radius: 12px;
            padding: 3rem;
            width: 100%;
            max-width: 400px;
            margin: 2rem;
            box-shadow: 0 4px 20px rgba(0,0,0,0.08);
        }

        .logo-section {
            text-align: center;
            margin-bottom: 2rem;
        }

        .logo {
            font-size: 2.5rem;
            margin-bottom: 0.5rem;
        }

        .title {
            font-size: 1.5rem;
            font-weight: 600;
            color: #2c3e50;
            margin-bottom: 0.5rem;
        }

        .subtitle {
            color: #7f8c8d;
            font-size: 0.9rem;
        }

        .form-group {
            margin-bottom: 1.5rem;
        }

        .form-label {
            display: block;
            margin-bottom: 0.5rem;
            color: #2c3e50;
            font-weight: 500;
        }

        .form-input {
            width: 100%;
            padding: 0.75rem 1rem;
            border: 2px solid #e9ecef;
            border-radius: 8px;
            font-size: 1rem;
            transition: all 0.3s;
            background: #f8fafc;
        }

        .form-input:focus {
            outline: none;
            border-color: #3498db;
            background: white;
            box-shadow: 0 0 0 3px rgba(52, 152, 219, 0.1);
        }

        .password-requirements {
            font-size: 0.8rem;
            color: #7f8c8d;
            margin-top: 0.5rem;
        }

        .register-btn {
            width: 100%;
            background: #3498db;
            color: white;
            border: none;
            padding: 0.75rem;
            border-radius: 8px;
            font-size: 1rem;
            font-weight: 600;
            cursor: pointer;
            transition: all 0.3s;
            margin-bottom: 1rem;
        }

        .register-btn:hover {
            background: #2980b9;
            transform: translateY(-1px);
            box-shadow: 0 4px 12px rgba(52, 152, 219, 0.3);
        }

        .register-btn:disabled {
            background: #bdc3c7;
            cursor: not-allowed;
            transform: none;
        }

        .auth-links {
            text-align: center;
            margin-top: 1.5rem;
        }

        .auth-link {
            color: #3498db;
            text-decoration: none;
            font-weight: 500;
            margin: 0 1rem;
            transition: color 0.3s;
        }

        .auth-link:hover {
            color: #2980b9;
        }

        .message {
            padding: 0.75rem;
            border-radius: 6px;
            margin-bottom: 1rem;
            font-size: 0.9rem;
            text-align: center;
            display: none;
        }

        .message.error {
            background: #f8d7da;
            color: #721c24;
            border: 1px solid #f5c6cb;
        }

        .message.success {
            background: #d4edda;
            color: #155724;
            border: 1px solid #c3e6cb;
        }

        .input-group {
            position: relative;
        }

        .password-toggle {
            position: absolute;
            right: 1rem;
            top: 50%;
            transform: translateY(-50%);
            background: none;
            border: none;
            color: #7f8c8d;
            cursor: pointer;
            font-size: 0.9rem;
        }

        @media (max-width: 480px) {
            .register-container {
                padding: 2rem;
                margin: 1rem;
            }

            .back-home {
                top: 1rem;
                left: 1rem;
            }
        }
    </style>
</head>

<body>
    <a href="index.html" class="back-home">← 返回首页</a>
    
    <div class="register-container">
        <div class="logo-section">
            <div class="logo"></div>
            <h1 class="title">创建账户</h1>
            <p class="subtitle">加入我们，开始购物吧</p>
        </div>

        <div id="message" class="message"></div>

        <form id="registerForm">
            <div class="form-group">
                <label class="form-label" for="username">用户名</label>
                <input type="text" class="form-input" id="username" name="username" required 
                       minlength="3" maxlength="20">
                <div class="password-requirements">3-20个字符，支持字母、数字和下划线</div>
            </div>

            <div class="form-group">
                <label class="form-label" for="password">密码</label>
                <div class="input-group">
                    <input type="password" class="form-input" id="password" name="password" required 
                           minlength="6">
                    <button type="button" class="password-toggle" onclick="togglePassword('password')">
                        显示
                    </button>
                </div>
                <div class="password-requirements">至少6个字符，建议包含字母和数字</div>
            </div>

            <div class="form-group">
                <label class="form-label" for="confirmPassword">确认密码</label>
                <div class="input-group">
                    <input type="password" class="form-input" id="confirmPassword" name="confirmPassword" required>
                    <button type="button" class="password-toggle" onclick="togglePassword('confirmPassword')">
                        显示
                    </button>
                </div>
            </div>

            <button type="submit" class="register-btn">注册</button>
        </form>

        <div class="auth-links">
            <a href="login.html" class="auth-link">已有账户？立即登录</a>
        </div>
    </div>

    <script>
        // 初始化
        document.addEventListener('DOMContentLoaded', function() {
            initializeForm();
        });

        // 初始化表单
        function initializeForm() {
            const form = document.getElementById('registerForm');
            
            form.addEventListener('submit', async function(e) {
                e.preventDefault();
                await handleRegister();
            });

            // 实时验证密码匹配
            const confirmPassword = document.getElementById('confirmPassword');
            confirmPassword.addEventListener('input', function() {
                validatePasswordMatch();
            });
        }

        // 处理注册
        async function handleRegister() {
            const username = document.getElementById('username').value.trim();
            const password = document.getElementById('password').value;
            const confirmPassword = document.getElementById('confirmPassword').value;

            // 基本验证
            if (!username || !password || !confirmPassword) {
                showMessage('请填写完整的注册信息', 'error');
                return;
            }

            if (username.length < 3 || username.length > 20) {
                showMessage('用户名长度应在3-20个字符之间', 'error');
                return;
            }

            if (!/^[a-zA-Z0-9_]+$/.test(username)) {
                showMessage('用户名只能包含字母、数字和下划线', 'error');
                return;
            }

            if (password.length < 6) {
                showMessage('密码长度至少为6个字符', 'error');
                return;
            }

            if (password !== confirmPassword) {
                showMessage('两次输入的密码不匹配', 'error');
                return;
            }

            const registerBtn = document.querySelector('.register-btn');
            registerBtn.disabled = true;
            registerBtn.textContent = '注册中...';

            try {
                const response = await fetch('/users/register', {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json',
                    },
                    body: JSON.stringify({
                        username: username,
                        password: password
                    })
                });

                const result = await response.json();

                if (result.success) {
                    showMessage('注册成功！正在跳转到登录页面...', 'success');
                    setTimeout(() => {
                        window.location.href = 'login.html';
                    }, 2000);
                } else {
                    showMessage(result.message || '注册失败，请重试', 'error');
                }
            } catch (error) {
                console.error('注册错误:', error);
                showMessage('网络错误，请稍后重试', 'error');
            } finally {
                registerBtn.disabled = false;
                registerBtn.textContent = '注册';
            }
        }

        // 验证密码匹配
        function validatePasswordMatch() {
            const password = document.getElementById('password').value;
            const confirmPassword = document.getElementById('confirmPassword').value;
            const confirmInput = document.getElementById('confirmPassword');

            if (confirmPassword && password !== confirmPassword) {
                confirmInput.style.borderColor = '#e74c3c';
            } else {
                confirmInput.style.borderColor = '#e9ecef';
            }
        }

        // 切换密码显示/隐藏
        function togglePassword(inputId) {
            const input = document.getElementById(inputId);
            const button = input.nextElementSibling;

            if (input.type === 'password') {
                input.type = 'text';
                button.textContent = '隐藏';
            } else {
                input.type = 'password';
                button.textContent = '显示';
            }
        }

        // 显示消息
        function showMessage(text, type = 'error') {
            const messageEl = document.getElementById('message');
            messageEl.textContent = text;
            messageEl.className = `message ${type}`;
            messageEl.style.display = 'block';

            setTimeout(() => {
                messageEl.style.display = 'none';
            }, 5000);
        }
    </script>
</body>

</html>